#ifndef DB_H
#define DB_H
#include <QObject>
#include <QSqlDatabase>
// #include "user.h"
class DB: public QObject
{
    Q_OBJECT
public:
    // 获取数据库实例
    static DB *getInstance();
    // 打开数据库
    void init();
    QHash<int, QByteArray>  getTableFieldRole(const QString& tableName);
    QString                 getParamValue(const QString &paramName) const;
    bool                    saveParamValue(const QString &paramName, const QString &value, const QString &update_by) const;
    bool                    insertParamDefault(const QString &paramName, const QString &value, const QString& desc) const;
    bool                    insertAlaramLog(const QString &type, const QString &desc, const QString& value, const QString& origin, int status) const;
    QHash<QString,QVariant> getSettingParams();
    QString                 getSettingParam(const QString& param);

    bool getAlarmParams(QHash<QString, QVariant> &speed, QHash<QString, QVariant> &temperature,QHash<QString, QVariant> &weight,QHash<QString, QVariant> &ph,QHash<QString, QVariant> &conductivity);
    QString getAlarmParam(const QString& param);
    bool saveAlarmAll(QHash<QString, QVariant> speed, QHash<QString, QVariant> weight, QHash<QString, QVariant> temperature
                      , QHash<QString, QVariant> ph, QHash<QString, QVariant> conductivity, const QString &update_by);
    bool saveAlarmParam(QHash<QString, QVariant> param, const QString &update_by, const QString& speed);

    QList<QHash<QString, QVariant>>     queryUsers(const QString& filter);
    QList<QHash<QString, QVariant>>     queryBatches(const QString& filter);
    QList<QHash<QString, QVariant>>     queryBatchData(const QString& beginTime, const QString& endTime);
    QList<QHash<QString, QVariant>>     queryOperation(const QString& filter);
    QList<QHash<QString, QVariant>>     queryWarn(const QString& filter);
    bool                                addUser(const QString &name,const QString password, const QString &real_name, const QString &expiration, const QString &status, const QString &permission, const QString &create_by);
    bool                                addBatch(const QString &name,const QString real_name, QString batchNo,
                  QString product, QString workShop, QString beginTime,
                  QString createBy);
    bool updateBatch(const QString desc, QString endTime, QString batchNo, QString product, QString workShop);
    bool updateUser(const QString &id, const QString &name,const QString password, const QString &real_name, const QString &expiration, const QString &status, const QString &permission, const QString &update_by);
    bool modifyPassword(const QString& id, const QString& password);

    bool addLog(const QString &name,const QString& realName, const QString &type, const QString &desc, const QString keyInfo, const QString &create_by);
    int isExist(const QString &name,const QString& type, const QString &desc);
    bool hasBeenUsed(int count, const QString &password, const QString &type, const QString &log_to, const QString &key_info);
    bool insertBatchData(QString name, QString speed, QString weight, QString temperature, QString ph, QString conductivity);
    // 关闭数据库
    void close();

private:
    // 禁用拷贝和移动构造函数
    Q_DISABLE_COPY_MOVE(DB)
    explicit DB(QObject *parent = nullptr);
    static DB *instance;
    QSqlDatabase m_db;
    bool isTableExists(const QString &tableName);
    bool initDB();
    bool initUser();
    bool initParam();
    bool initAlarm();
    bool initAlarmLog();
    bool initLog();
    bool initBatch();
    bool initBatchData();
};

#endif // DBHELPER_H
